
const SHA256 = require("crypto-js/sha256");
const BASE64 = require("crypto-js/enc-base64");


const encryption = pwd => BASE64.stringify(SHA256(pwd));
const getElementById = id => document.getElementById(id);

/**POST方法*/
const post = (url, data, cb) => {
    let xhr = new XMLHttpRequest();
    xhr.open('POST', url, false);
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.onreadystatechange = () => {
        if (xhr.readyState == 4) {
            cb(JSON.parse(xhr.responseText), xhr.status, xhr);
        }
    }
    xhr.send(data && JSON.stringify(data));
}

/**登录绑定*/
getElementById('login_btn').onclick = () => {
    let u = getElementById('username').value;
    let p = getElementById('password').value;
    post('/--/login-info', 0, info => {
        let S = info.salt;
        let s = `${info.time}.${Math.random()}`;
        p = encryption(encryption(p + S).toLowerCase() + s).toLowerCase();
        post('/--/login', { username: u, password: p, salt: s }, result => {
            if (result.success) return window.location.reload();//刷新页面
            alert(result.result);
        });
    });
};